﻿using System.Collections.Generic;
using System.Text;
using UnityEditor;
using UnityEngine;


public class ExcelToJson : EditorWindow
{
    ///当前编辑器窗口实例
    private static ExcelToJson instance;

    ///Excel文件列表
    private static List<string> excelList;

    ///项目根路径
    private static string pathRoot;

    ///滚动窗口初始位置
    private static Vector2 scrollPos;

    //编码索引
    private static int indexOfEncoding = 0;

    ///编码选项
    private static readonly string[] encodingOption = new string[] { "UTE-8", "GB2312" };


    ///是否保留源文件
    private static bool keepSource = true;

    [MenuItem("Plugins/ExcelTools")]
    static void ShowExcelTools()
    {
        Init();

        LoadExcel();
        instance.Show();
    }

    private void OnGUI()
    {
        DrawOptions();
        DrawExport();
    }

    private void DrawOptions()
    {
        GUILayout.BeginHorizontal();
        EditorGUILayout.LabelField("请选择编码类型:", GUILayout.Width(85));
        indexOfEncoding = EditorGUILayout.Popup(indexOfEncoding, encodingOption, GUILayout.Width(125));
        GUILayout.EndHorizontal();


        keepSource = GUILayout.Toggle(keepSource, "保留Excel源文件");
    }



    ///转换Excel文件
    private static void Convert()
    {
        foreach (string assetsPath in excelList)
        {
            //获取Excel文件绝对路径
            string excelPath = pathRoot + "/" + assetsPath;
            //构造Excel工具类
            Excel excel = new Excel(excelPath);

            //判断编码类型
            Encoding encoding = null;
            if (indexOfEncoding == 0 || indexOfEncoding == 3)
            {
                encoding = Encoding.GetEncoding("utf-8");
            }
            else if (indexOfEncoding == 1)
            {
                encoding = Encoding.GetEncoding("gb2312");
            }

            string output = excelPath.Replace(".xlsx", ".json");
            excel.ConvertToJson(output, encoding);

            //判断是否保留文件
            if (!keepSource)
            {
                FileUtil.DeleteFileOrDirectory(excelPath);
            }
            //刷新本地资源
            AssetDatabase.Refresh();
        }
        //转换完后关闭插件
        //这样是为了解决窗口再次点击时路径错误的bug
        instance.Close();
    }
    ///绘制插件界面输出项
    private void DrawExport()
    {
        if (excelList == null)
            return;

        if (excelList.Count < 1)
        {
            EditorGUILayout.LabelField("目前没有文件被选中哦!");
        }
        else
        {
            EditorGUILayout.LabelField("下列项目将被转换为Json:");
            GUILayout.BeginHorizontal();
            scrollPos = GUILayout.BeginScrollView(scrollPos, false, true, GUILayout.Height(150));
            foreach (string s in excelList)
            {
                GUILayout.BeginHorizontal();
                GUILayout.Toggle(true, s);
                GUILayout.EndHorizontal();
            }
            GUILayout.EndScrollView();
            GUILayout.EndVertical();

            if (GUILayout.Button("转换"))
            {
                Convert();
            }
        }

    }
    private static void LoadExcel()
    {
        if (excelList == null)
            excelList = new List<string>();
        excelList.Clear();
        //获取选中的对象
        object[] selection = (object[])Selection.objects;
        //判断是否有对象被选中
        if (selection.Length == 0)
            return;
        //遍历每一对象判断是不是Excel文件
        foreach (Object obj in selection)
        {
            string objPath = AssetDatabase.GetAssetPath(obj);
            if (objPath.EndsWith(".xlsx"))
            {
                excelList.Add(objPath);
            }
        }
    }

    private static void Init()
    {
        //获取当前实例
        instance = EditorWindow.GetWindow<ExcelToJson>();
        //初始化
        pathRoot = Application.dataPath;
        //注意这里需要对路径进行处理，目的是去除Assets这部分字符以获取项目目录
        pathRoot = pathRoot.Substring(0, pathRoot.LastIndexOf("/"));
        excelList = new List<string>();
        scrollPos = new Vector2(instance.position.x, instance.position.y + 75);

    }

    private void OnSelectionChange()
    {
        //当选择发生变化时重绘窗体
        Show();
        LoadExcel();
        Repaint();
    }

}
